<%@ Page Language="C#" Inherits="System.Web.Mvc.ViewPage<Page>" %>
<%@ Import Namespace="Buffalo.Administration.DAL" %>
<%@ Import Namespace="Buffalo.Administration.Enum" %>
<%@ Import Namespace="Buffalo.Tourism.DAL" %>
<%@ Import Namespace="Buffalo.Tourism.Enum" %>
<%@ Import Namespace="Buffalo.Web.Areas.Tourism" %>

<%@ Register Assembly="Ext.Net" Namespace="Ext.Net" TagPrefix="ext" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
    <title></title>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />

    <script runat="server">
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!this.IsPostBack)
            {
                bool hasEditPermission = new PermissionDAL().HasPermission(GeneralVar.gLoggedUserName, "TourListView", enuPermissionType.Edit);
                btnAdd.Visible = hasEditPermission;

                hidHasEditPermission.Value = hasEditPermission;
            }
        }
        protected void btnRefresh_Click(object sender, DirectEventArgs e)
        {
            new TourProgramDAL().UpdateTourStatus();
        }
    </script>
    <script type="text/javascript">
        var openImportExcelHandler = function () {
            parent.AppGeneral.addTab({ title: 'Import Excel', url: '/Areas/Tourism/Pages/ImportExcelView.aspx', icon: 'Form' });

        };
        var openAddTourHandler = function () {
            var win = TourEditorWindow;
            win.autoLoad.params.IDTour = '0';
            win.setTitle('Add Tour');
            win.show();
        };
        var commandHandler = function (cmd, record) {
            switch (cmd) {
                case "edit":
                    var win = TourEditorWindow;
                    win.autoLoad.params.IDTour = record.id;
                    win.setTitle('Edit Tour: ' + record.data.TourCode);
                    win.show();
                    break;
                case "delete":
                    if (hidHasEditPermission.getValue() != "true")
                        return;
                    Ext.Msg.confirm('Warning', 'Delete Tour?', function (btn) {
                        if (btn == "yes") {
                            dsTour.remove(record);
                            dsTour.save();
                        }
                    });
                    break;
            }
        };
        function GetMonthFromMonthText(monthText) {
            var monthNum = 0;
            switch (monthText) {
                case "Jan":
                    monthNum = "01";
                    break;
                case "Feb":
                    monthNum = "02";
                    break;
                case "Mar":
                    monthNum = "03";
                    break;
                case "Apr":
                    monthNum = "04";
                    break;
                case "May":
                    monthNum = "05";
                    break;
                case "Jun":
                    monthNum = "06";
                    break;
                case "Jul":
                    monthNum = "07";
                    break;
                case "Aug":
                    monthNum = "08";
                    break;
                case "Sep":
                    monthNum = "09";
                    break;
                case "Oct":
                    monthNum = "10";
                    break;
                case "Nov":
                    monthNum = "11";
                    break;
                case "Dec":
                    monthNum = "12";
                    break;
            }
            return monthNum;
        }
        function render(value, metadata, record, rowIndex, colIndex, store) {
            if (colIndex < 2)
                return;
            var retVal;
            switch (record.data.Status) {
                case 1:
                    retVal = colIndex == 10 ? 'Booked' : value;
                    break;
                case 2:
                    metadata.style += "background-color: #FFAABB;";
                    retVal = colIndex == 10 ? 'Done' : value;
                    break;
                case 3:
                    metadata.style += "background-color: #FFFAC8;";
                    retVal = colIndex == 10 ? 'Running' : value;
                    break;
                default:
                    retVal = value;
            }
            if (colIndex == 6 || colIndex == 7 || colIndex == 9) {
                if (value) {
                    var fullDate = value.toString().substring(0, 15);
                    var monthText = fullDate.substring(4, 7);
                    var month = GetMonthFromMonthText(monthText);
                    var day = fullDate.substring(8, 10);
                    var year = fullDate.substring(11, 15);
                    if (year != '1900')
                        retVal = day + "/" + month + "/" + year;
                    else
                        retVal = '';
                }
            }
            return retVal;
        }
    </script>
</head>
<body>
    <ext:ResourceManager ID="ResourceManager1" runat="server" />
    <ext:Store ID="dsTour" runat="server" RemoteSort="true" UseIdConfirmation="false"
        AutoLoad="false">
        <Proxy>
            <ext:HttpProxy Url="/TourProgram/GetListTour/" />
        </Proxy>
        <UpdateProxy>
            <ext:HttpWriteProxy Url="/TourProgram/SaveTours/" />
        </UpdateProxy>
        <Reader>
            <ext:JsonReader IDProperty="ID" Root="data" TotalProperty="total">
                <Fields>
                    <ext:RecordField Name="ID" />
                    <ext:RecordField Name="TourCode" />
                    <ext:RecordField Name="Name" />
                    <ext:RecordField Name="TourCodeDate" />
                    <ext:RecordField Name="Departure" />
                    <ext:RecordField Name="Destination" />
                    <ext:RecordField Name="ChildrenNum" />
                    <ext:RecordField Name="TourType" />
                    <ext:RecordField Name="GuestNum" />
                    <ext:RecordField Name="RoomNum" />
                    <ext:RecordField Name="StartDate" Type="Date" DateFormat="yyyy-MM-ddThh:mm:ss" />
                    <ext:RecordField Name="EndDate" Type="Date" DateFormat="yyyy-MM-ddThh:mm:ss" />
                    <ext:RecordField Name="IDNationality" />
                    <ext:RecordField Name="NationalityName" />
                    <ext:RecordField Name="IDBroker" />
                    <ext:RecordField Name="BrokerName" />
                    <ext:RecordField Name="IDOwnCompany" />
                    <ext:RecordField Name="OwnCompanyName" />
                    <ext:RecordField Name="IDLanguage" />
                    <ext:RecordField Name="LanguageName" />
                    <ext:RecordField Name="FeedbackGuestName" />
                    <ext:RecordField Name="FeedbackGuestFrom" />
                    <ext:RecordField Name="FeedbackGuestJob" />
                    <ext:RecordField Name="FeedbackGuestEmail" />
                    <ext:RecordField Name="FeedbackNote" />
                    <ext:RecordField Name="FeedbackOther" />
                    <ext:RecordField Name="FeedbackTourProgress" />
                    <ext:RecordField Name="Status" />
                    <ext:RecordField Name="Note" />
                    <ext:RecordField Name="Route" />
                    <ext:RecordField Name="CreatedDate" Type="Date" DateFormat="yyyy-MM-ddThh:mm:ss" />
                    <ext:RecordField Name="CreatedBy" />
                    <ext:RecordField Name="LastUpdatedDate" />
                    <ext:RecordField Name="LastUpdatedBy" />
                    <ext:RecordField Name="IDOrgUnit" />
                    <ext:RecordField Name="IsOrgUnitLimit" />
                    <ext:RecordField Name="LocationRoleName" />
                </Fields>
            </ext:JsonReader>
        </Reader>
        <BaseParams>
            <ext:Parameter Name="limit" Value="20" Mode="Raw" />
            <ext:Parameter Name="start" Value="0" Mode="Raw" />
            <ext:Parameter Name="dir" Value="ASC" Mode="Value" />
            <ext:Parameter Name="sort" Value="Name" Mode="Value" />
            <ext:Parameter Name="cboCriteria" Value="#{cboCriteria}.getValue()" Mode="Raw" />
            <ext:Parameter Name="txtSearchText" Value="#{txtSearchText}.getValue()" Mode="Raw" />
        </BaseParams>
        <SortInfo Field="TourCode" Direction="ASC" />
    </ext:Store>
    <ext:Viewport ID="ViewPort1" runat="server" Layout="fit">
        <Items>
            <ext:GridPanel ID="grdPanTourList" runat="server" Header="false" Border="false" TrackMouseOver="true"
                ClicksToEdit="1" AutoExpandColumn="TourCode" StoreID="dsTour">
                
                <ColumnModel ID="ColumnModel1" runat="server">
                    <Columns>
                        <ext:CommandColumn Width="25" Hideable="false">
                            <Commands>
                                <ext:GridCommand CommandName="edit" Icon="ApplicationFormEdit">
                                    <ToolTip Text="Edit" />
                                </ext:GridCommand>
                            </Commands>
                            <PrepareToolbar Handler="toolbar.setVisible(!record.newRecord);" />
                        </ext:CommandColumn>
                        <ext:CommandColumn Width="25" Hideable="false">
                            <Commands>
                                <ext:GridCommand CommandName="delete" Icon="Cross">
                                    <ToolTip Text="Delete" />
                                </ext:GridCommand>
                            </Commands>
                            <PrepareToolbar Handler="toolbar.setVisible(!record.newRecord);" />
                        </ext:CommandColumn>
                        <ext:Column ColumnID="TourCode" DataIndex="TourCode" Header="TourCode" >
                            <Renderer Fn="render" />
                        </ext:Column>
                        <ext:Column ColumnID="OwnCompanyName" DataIndex="OwnCompanyName" Header="TravelAgency" >
                            <Renderer Fn="render" />
                        </ext:Column>
                        <ext:Column ColumnID="LanguageName" DataIndex="LanguageName" Header="Language" >
                            <Renderer Fn="render" />
                        </ext:Column>
                        <ext:Column ColumnID="GuestNum" DataIndex="GuestNum" Header="GuestNum" >
                            <Renderer Fn="render" />
                        </ext:Column>
                        <ext:Column ColumnID="StartDate" DataIndex="StartDate" Header="StartDate" >
                            <Renderer Fn="render" />
                        </ext:Column>
                        <ext:Column ColumnID="EndDate" DataIndex="EndDate" Header="EndDate" >
                            <Renderer Fn="render" />
                        </ext:Column>
                        <ext:Column ColumnID="BrokerName" DataIndex="BrokerName" Header="Sales" >
                            <Renderer Fn="render" />
                        </ext:Column>
                        <ext:Column ColumnID="CreatedDate" DataIndex="CreatedDate" Header="CreatedDate" >
                            <Renderer Fn="render" />
                        </ext:Column>
                        <ext:Column Header="Status" DataIndex="Status">
                            <Renderer Fn="render" />
                        </ext:Column>
                    </Columns>
                </ColumnModel>
                <TopBar>
                    <ext:Toolbar ID="Toolbar1" runat="server">
                        <Items>
                            <ext:Button ID="btnAdd" runat="server" Text="Add" Icon="Add">
                                <Listeners>
                                    <Click Handler="openAddTourHandler();" />
                                </Listeners>
                            </ext:Button>
                            <ext:Button ID="btnImportExcel" runat="server" Text="Import from Excel" Icon="PageExcel">
                                <Listeners>
                                    <Click Handler="openImportExcelHandler();" />
                                </Listeners>
                            </ext:Button>
                            <ext:ToolbarFill ID="ToolbarFill1" runat="server" />
                            <ext:SelectBox ID="cboCriteria" runat="server" EmptyText="Select...">
                                <Items>
                                    <ext:ListItem Text="..." Value="All" />
                                    <ext:ListItem Text="Month" Value="ByMonth" />
                                    <ext:ListItem Text="Tour Code" Value="ByTourCode" />
                                    <ext:ListItem Text="Guest Number" Value="ByGuestNum" />
                                    <ext:ListItem Text="Start Date" Value="ByStartDate" />
                                    <ext:ListItem Text="End Date" Value="ByEndDate" />
                                    <ext:ListItem Text="Nationality" Value="ByNationalityName" />
                                    <ext:ListItem Text="Company" Value="ByOwnCompanyName" />
                                    <ext:ListItem Text="Language" Value="ByLanguageName" />
                                    <ext:ListItem Text="Sale" Value="ByBrokerName" />
                                    <ext:ListItem Text="Tour Program" Value="ByTourProgram" />
                                    <ext:ListItem Text="Restaurant" Value="ByRestaurantName" />
                                    <ext:ListItem Text="TourGuide" Value="ByTourGuideName" />
                                    <ext:ListItem Text="Vehicle" Value="ByVehicleName" />
                                    <ext:ListItem Text="Hotel" Value="ByHotelName" />
                                    <ext:ListItem Text="TourGuide" Value="ByTourGuideName" />
                                    <ext:ListItem Text="Partner" Value="ByPartnerName" />
                                </Items>
                            </ext:SelectBox>
                            <ext:ToolbarSpacer ID="ToolbarSpacer2" runat="server" />
                            <ext:TextField ID="txtSearchText" runat="server" AnchorHorizontal="100%" EmptyText="Search..." />
                            <ext:ToolbarSpacer ID="ToolbarSpacer3" runat="server" />
                            <ext:Button ID="btnSearch" runat="server" Text="Search" Icon="Find">
                                <Listeners>
                                    <Click Handler="dsTour.reload();" />
                                </Listeners>
                            </ext:Button>
                        </Items>
                    </ext:Toolbar>
                </TopBar>
                <SelectionModel>
                    <ext:RowSelectionModel ID="RowSelectionModel1" runat="server" />
                </SelectionModel>
                <BottomBar>
                    <ext:PagingToolbar ID="PagingToolbar1" runat="server" PageSize="20" />
                </BottomBar>
                <Listeners>
                    <Command Fn="commandHandler" />
                </Listeners>
                <LoadMask ShowMask="true" />
                <SaveMask ShowMask="true" />
            </ext:GridPanel>
            <ext:Hidden ID="hidHasEditPermission" runat="server"></ext:Hidden>
        </Items>
    </ext:Viewport>
    <ext:Window ID="TourEditorWindow" runat="server" Icon="ApplicationFormEdit" Width="1000"
        Height="500" Hidden="true" Modal="true" Constrain="true">
        <AutoLoad Url="/Tourism/TourProgram/TourEditor/" Mode="IFrame" TriggerEvent="show"
            ReloadOnEvent="true" ShowMask="true" MaskMsg="Loading Tour program...">
            <Params>
                <ext:Parameter Name="IDTour" Value="" Mode="Value" />
            </Params>
        </AutoLoad>
    </ext:Window>
    <ext:Window ID="ImportExcelWindow" runat="server" Icon="ArrowIn" Width="1000" Height="500"
        Hidden="true" Modal="true" Constrain="true">
        <AutoLoad Url="/Tourism/TourProgram/ImportExcelView/" Mode="IFrame" TriggerEvent="show"
            ReloadOnEvent="true" ShowMask="true" MaskMsg="Opening...">
        </AutoLoad>
    </ext:Window>
</body>
</html>
